Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
TypeDI is a dependency injection tool for TypeScript and JavaScript. It allows you to manage the lifecycle and dependencies of your services and components in a clean and efficient way.
Service Decorator
The `@Service` decorator is used to mark a class as a service that can be injected. The `Container.get` method is then used to retrieve an instance of the service.
```typescript
import { Service } from 'typedi';
@Service()
class ExampleService {
sayHello() {
return 'Hello, World!';
}
}
const exampleService = Container.get(ExampleService);
console.log(exampleService.sayHello()); // Output: Hello, World!
```
Inject Decorator
The `@Inject` decorator is used to inject a dependency into a class. In this example, `DependencyService` is injected into `MainService`.
```typescript
import { Service, Inject } from 'typedi';
@Service()
class DependencyService {
getValue() {
return 'Dependency Value';
}
}
@Service()
class MainService {
constructor(@Inject(() => DependencyService) private dependencyService: DependencyService) {}
getValue() {
return this.dependencyService.getValue();
}
}
const mainService = Container.get(MainService);
console.log(mainService.getValue()); // Output: Dependency Value
```
Container
The `Container` class is used to manage the lifecycle of services. You can manually set and get instances of services using `Container.set` and `Container.get`.
```typescript
import { Container } from 'typedi';
class ExampleService {
sayHello() {
return 'Hello, World!';
}
}
Container.set(ExampleService, new ExampleService());
const exampleService = Container.get(ExampleService);
console.log(exampleService.sayHello()); // Output: Hello, World!
```
Inversify is a powerful and flexible inversion of control (IoC) container for JavaScript and TypeScript. It provides a similar feature set to TypeDI, including decorators for services and dependency injection. Inversify is known for its extensive documentation and strong community support.
TSyringe is a lightweight dependency injection container for TypeScript. It offers a similar decorator-based API for defining and injecting services. TSyringe is designed to be simple and easy to use, making it a good choice for smaller projects or those new to dependency injection.
Awilix is a dependency injection container for JavaScript and TypeScript that focuses on developer experience and ease of use. It provides a fluent API for defining and resolving dependencies, and supports both class-based and function-based services. Awilix is known for its flexibility and ease of integration with various frameworks.
Dependency injection tool for Typescript.
If you simply want to use a container:
class SomeClass {
someMethod() {
}
}
let someClass = Container.get(SomeClass);
someClass.someMethod();
If you want to inject other classes into your service you can do:
class BeanFactory {
create() {
}
}
class SugarFactory {
create() {
}
}
class WaterFactory {
create() {
}
}
class CoffeeMaker {
@Inject()
beanFactory: BeanFactory;
@Inject()
sugarFactory: SugarFactory;
@Inject()
waterFactory: WaterFactory;
make() {
this.beanFactory.create();
this.sugarFactory.create();
this.waterFactory.create();
}
}
let coffeeMaker = Container.get(CoffeeMaker);
coffeeMaker.make();
If you want to use constructor injection:
class BeanFactory {
create() {
}
}
class SugarFactory {
create() {
}
}
class WaterFactory {
create() {
}
}
@Resolve()
class CoffeeMaker {
private beanFactory: BeanFactory;
private sugarFactory: SugarFactory;
private waterFactory: WaterFactory;
constructor(beanFactory: BeanFactory, sugarFactory: SugarFactory, waterFactory: WaterFactory) {
this.beanFactory = beanFactory;
this.sugarFactory = sugarFactory;
this.waterFactory = waterFactory;
}
make() {
this.beanFactory.create();
this.sugarFactory.create();
this.waterFactory.create();
}
}
let coffeeMaker = Container.get(CoffeeMaker);
coffeeMaker.make();
That's should be enough to use a proper dependency injection.
Also you can inject a modules that you want to require
:
class SomeClass {
someMethod() {
}
}
@Resolve()
class CoffeeMaker {
private someClass: SomeClass;
private gulp: any; // you can use type if you have definition for this package
constructor(someClass: SomeClass, @Require('gulp') gulp: any) {
this.someClass = someClass;
this.gulp = gulp; // the same if you do this.gulp = require('gulp')
}
make() {
this.someClass.someMethod();
console.log(this.gulp); // here you get console.logged gulp package =)
}
}
let coffeeMaker = Container.get(CoffeeMaker);
coffeeMaker.make();
Take a look on samples in ./sample
for more examples of usages.
FAQs
Dependency injection for TypeScript.
We found that typedi demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.